5 // Created by J Osborne on 7/28/13.
9 #import "KNPersistantState.h"
11 @implementation KNPersistantState
13 -(NSURL*)urlForStorage
15 return [NSURL URLWithString:@"Preferences/com.apple.security.KCN.plist" relativeToURL:[[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
18 +(instancetype)loadFromStorage
20 KNPersistantState *state = [[KNPersistantState alloc] init];
25 id plist = @{@"lastWritten": [NSDate distantPast]};
28 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
30 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
32 NSPropertyListFormat format;
33 plist = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
36 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
40 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
41 state.lastWritten = plist[@"lastWritten"];
42 state.pendingApplicationReminderInterval = plist[@"pendingApplicationReminderInterval"];
43 state.debugLeftReason = plist[@"debugLeftReason"];
44 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"];
45 state.applcationDate = plist[@"applcationDate"];
46 if (!state.applcationDate) {
47 state.applcationDate = [NSDate distantPast];
49 if (!state.pendingApplicationReminder) {
50 state.pendingApplicationReminder = [NSDate distantFuture];
52 if (!state.pendingApplicationReminderInterval || [state.pendingApplicationReminderInterval doubleValue] <= 0) {
53 state.pendingApplicationReminderInterval = [NSNumber numberWithUnsignedInt:60 * 60 * 24 * 2];
61 NSMutableDictionary *plist = [@{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
62 @"lastWritten": [NSDate date],
63 @"applcationDate": self.applcationDate,
64 @"pendingApplicationReminder": self.pendingApplicationReminder,
66 if (self.debugLeftReason) {
67 plist[@"debugLeftReason"] = self.debugLeftReason;
69 if (self.pendingApplicationReminderInterval) {
70 plist[@"pendingApplicationReminderInterval"] = self.pendingApplicationReminderInterval;
72 NSLog(@"writeToStorage plist=%@", plist);
75 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:[plist copy] format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
77 NSLog(@"Can't serialize %@: %@", plist, error);
80 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
81 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);